package com.skyolder.milkbee.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.skyolder.milkbee.domain.Follow;

import com.skyolder.milkbee.repository.FollowRepository;
import com.skyolder.milkbee.web.rest.util.HeaderUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Follow.
 */
@RestController
@RequestMapping("/api")
public class FollowResource {

    private final Logger log = LoggerFactory.getLogger(FollowResource.class);
        
    @Inject
    private FollowRepository followRepository;

    /**
     * POST  /follows : Create a new follow.
     *
     * @param follow the follow to create
     * @return the ResponseEntity with status 201 (Created) and with body the new follow, or with status 400 (Bad Request) if the follow has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/follows")
    @Timed
    public ResponseEntity<Follow> createFollow(@RequestBody Follow follow) throws URISyntaxException {
        log.debug("REST request to save Follow : {}", follow);
        if (follow.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("follow", "idexists", "A new follow cannot already have an ID")).body(null);
        }
        Follow result = followRepository.save(follow);
        return ResponseEntity.created(new URI("/api/follows/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("follow", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /follows : Updates an existing follow.
     *
     * @param follow the follow to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated follow,
     * or with status 400 (Bad Request) if the follow is not valid,
     * or with status 500 (Internal Server Error) if the follow couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/follows")
    @Timed
    public ResponseEntity<Follow> updateFollow(@RequestBody Follow follow) throws URISyntaxException {
        log.debug("REST request to update Follow : {}", follow);
        if (follow.getId() == null) {
            return createFollow(follow);
        }
        Follow result = followRepository.save(follow);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("follow", follow.getId().toString()))
            .body(result);
    }

    /**
     * GET  /follows : get all the follows.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of follows in body
     */
    @GetMapping("/follows")
    @Timed
    public List<Follow> getAllFollows() {
        log.debug("REST request to get all Follows");
        List<Follow> follows = followRepository.findAll();
        return follows;
    }

    /**
     * GET  /follows/:id : get the "id" follow.
     *
     * @param id the id of the follow to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the follow, or with status 404 (Not Found)
     */
    @GetMapping("/follows/{id}")
    @Timed
    public ResponseEntity<Follow> getFollow(@PathVariable Long id) {
        log.debug("REST request to get Follow : {}", id);
        Follow follow = followRepository.findOne(id);
        return Optional.ofNullable(follow)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /follows/:id : delete the "id" follow.
     *
     * @param id the id of the follow to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/follows/{id}")
    @Timed
    public ResponseEntity<Void> deleteFollow(@PathVariable Long id) {
        log.debug("REST request to delete Follow : {}", id);
        followRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("follow", id.toString())).build();
    }

}
